<?php


namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Residential extends Model
{
    use HasFactory,SoftDeletes;
    public $table = 'fang_publish';


    public static function getOne($params)
    {
        return self::where($params)->get()->toArray();
    }

    protected $fillable = ['logo','location','region','price','audit','area'];

    public static function GetPaginate($res){
        return self::query()->select($res)->paginate(10);
    }

    public static function GetCreate($data){
        return self::query()->create($data);
    }

    public static function Getfirst($result){
        return self::query()->where($result)->first();
    }

    public static function GetPaginates(){
        return self::query()->where('is_free',1)->paginate(5);
    }


    public static function GetUpdate($data){
        return self::query()->where('id',$data['data']['id'])->update($data['data']);
    }

    public static function GetDestroy($id){
        return self::query()->destroy($id);
    }

    public static function wheres($where){
        return self::whereIn('loaction',$where)->get();
    }

    public static function detail($id)
    {
        return self::where('is_del','0')->find($id);
    }

    public static function GetAll()
    {
        return self::where('is_del','0')->paginate(10)->toArray();
    }

    //多条件搜索分页
    public static function getRz($limit,$offset){

        $data['data']=self::where('house_type', 1)->where('is_del', 0)
            ->offset($offset)//offset是偏移量
            ->limit($limit)//limit展示每页条数
            ->get()
            ->toArray();

        $data['total'] = self::where('is_del', 0)->where('house_type', 1)->count();//offset是偏移量; //mit展示每页条数;
        $data['limit'] =(int)$limit;
        $data['last_page']  = ceil($data['total']/ $data['limit'] =(int)$limit);
        return $data;
    }

    //推荐房源
    public static function getRecommendHouse()
    {
        $data = self::where('is_recommend', 1)->get()->toArray();
        return $data;
    }



    //多条件搜索分页
    public static function getSecharvr($params, $limit, $offset)
    {
        $data['data']= self::when($params, function ($query) use ($params) {
            if (isset($params['location']) && !empty($params['location'])) {
                $query->where('location', 'like', '%' . $params['location'] . '%');
            }
            if (isset($params['price']) && !empty($params['phone'])) {
                $query->Where('price', '>',$params['price']);
            }
        })->offset($offset)->limit($limit)->get();//offset是偏移量; //mit展示每页条数

        $data['total'] =(int)self::when($params, function ($query) use ($params) {
            if (isset($params['location']) && !empty($params['location'])) {
                $query->where('location', 'like', '%' . $params['location'] . '%');
            }
            if (isset($params['price']) && !empty($params['phone'])) {
                $query->Where('price', '>',$params['price']);
            }
        })->count();//offset是偏移量; //mit展示每页条数;

        $data['limit'] =(int)$limit;
        return $data;
    }
    public static function GetHouse($where)
    {
        return self::where($where)->where('is_del',0)->get()->toArray();
    }
}
